﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using deviceManage.Models;
using deviceManage.vo;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using deviceManage.Filters;
using deviceManage.Tools;
using deviceManage.VO;
using deviceManage.Service;
using System.IO;
using OfficeOpenXml;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.Drawing;
using System.Text;
using System.Globalization;

namespace deviceManage.Controllers
{
    [CheckLoginAttribute]
    public class HomeController : Controller
    {
        DeviceCtx _context;

        public HomeController(DeviceCtx ctx)
        {
            this._context = ctx;
        }

        public IActionResult Index()
        {
            User user = null;
            var userId = HttpContext.Session.GetString(WebContext.SESSION_KEY_USER_ID);
            user = _context.User.Single(p => p.Id == userId);

            //获取授权的菜单
            Dictionary<String, Menu> menuDict = new Dictionary<string, Menu>();
            var roleUserList  = this._context.RoleUser.Include(p=>p.Role).Where(p => p.UserId == userId).ToList();
            var viewFlag = 0; //只读标志
            foreach(var roleUser in roleUserList)
            {
                var roleMenuList = this._context.RoleMenu.Include(p=>p.Men)
                    .Where(p => p.RoleId == roleUser.RoleId).ToList();
                foreach(var roleMenu in roleMenuList)
                {
                    if (!menuDict.Keys.Contains(roleMenu.MenId)) {
                        menuDict.Add(roleMenu.MenId, roleMenu.Men);                        
                    }   
                }

                if(roleUser.Role.view_flag != null && roleUser.Role.view_flag == 1)
                {
                    viewFlag = 1;
                }
            }

            HttpContext.Session.SetInt32("viewFlag", viewFlag);


            //对菜单进行排序
            List<Menu> mList = new List<Menu>();
            foreach (var menu in menuDict.Values)
            {
                if (menu.Level == 1) mList.Add(menu);
                foreach (var subMenu in menuDict.Values)
                {
                    if (subMenu.Level == 2 && subMenu.ParentId == menu.Id)
                    {
                        menu.subMenus.Add(subMenu);
                    }
                }
                menu.subMenus.Sort((a,b)=>a.sort_order-b.sort_order);
            }
            mList.Sort((a, b) => a.sort_order - b.sort_order);

            return View(Tuple.Create(user,mList));
        } 

        public IActionResult Dashboard()
        {
            return View();
        }

        /// <summary>
        /// 获取异常设备列表
        /// </summary>
        /// <returns></returns>
        public AjaxResult GetBadDeviceList()
        {
            AjaxResult result = new AjaxResult();
            IQueryable<Device> q = _context.Device.Where(p => p.Status == DeviceState.STATE_BAD);
            result.Code = 0;
            result.Count = 10;
            result.Data = q.Include(p=>p.Room)
                .Take(10).OrderBy(p => p.Name).ToList();
            return result;
        }


        /// <summary>
        /// 获取设备运维计划
        /// </summary>
        /// <returns></returns>
        public AjaxResult GetDevicePlan()
        {
            AjaxResult result = new AjaxResult();
            IQueryable<DevicePlan> q = _context.DevicePlan;
            result.Code = 0;
            result.Count = 10;
            result.Data = q.Take(10).OrderBy(p =>p.StartTime).ToList();
            return result;
        } 
        
    }
}
